﻿@{
    var service = UrlData[0].ToLower();;
    var id = UrlData[1];
    var data = new object[2];
    bool res=false;
    IList<dynamic> rList=null;
    
    string role=string.Empty;
    string user=string.Empty;
    switch(service){
        case "getusers":
              var users = AccountManager.GetUsers();
              data[0]=true;
              data[1]=users;
        break;
        case "getroles":
            var roles = AccountManager.GetRoles();
            data[0]=true;
            data[1]=roles;
        break;
        case "adduser":
            user = Request["uname"];
            string pass=Request["pass"];
            res = AccountManager.AddUser(user,pass);
            rList = AccountManager.GetUsers();
            data[0]=res;
            data[1]=rList;
        break;
            
        case "removeuser":
            user = Request["user"];
            res = AccountManager.RemoveUser(Convert.ToInt32(user));
            rList = AccountManager.GetUsers();
            data[0]=res;
            data[1]=rList;
            break;
        
        case "addrole":
            role = Request["role"];
            res  = AccountManager.AddRole(role);
            rList = AccountManager.GetRoles();
            data[0]=res;
            data[1]=rList;
            break;
        case "removerole":
            role =Request["roles"];
            res = AccountManager.RemoveRole(Convert.ToInt32(role));
            rList = AccountManager.GetRoles();
            data[0]=res;
            data[1]=rList;  
            break;
          
        case "addrm":
            user = Request["user"];
            role = Request["roles"];
            //get all checked users
            string[] rols = role.Split(',');
            foreach(var r in rols){
                res = AccountManager.AddUserToRole(Convert.ToInt32(user),Convert.ToInt32(r));
            }
            
            data[0]=res;
            data[1]=AccountManager.GetUserRoles(Convert.ToInt32(user));
        break;
        case "removeurm":
            user = Request["user"];
            role = Request["roles"];
            string[] urs=role.Split(',');
            foreach(var ur in urs){
                res = AccountManager.RemoveUserInRole(Convert.ToInt32(user),Convert.ToInt32(ur));
            }
            data[0]=res;
            data[1]=AccountManager.GetUserRoles(Convert.ToInt32(user));
        break;
            
        case "geturoles":
            user = Request["user"];
            var uroles = AccountManager.GetUserRoles(Convert.ToInt32(user));
            data[0]=true;
            data[1]=uroles;
            break;
        case "getmembers":
            int roleid = Convert.ToInt32(Request["roleid"]);
            var members = AccountManager.GetRoleMembers(roleid);
            data[0]=true;
            data[1]=members;
        break;
        case "getrmembers":
            int rid = Convert.ToInt32(Request["rls"]);
            var rmembers = AccountManager.GetRoleMembers(rid);
            data[0]=true;
            data[1]=rmembers;
            break;
        case "rm":
            string rms = Request["members"];
            role = Request["rls"];
            string[] mbers=rms.Split(',');
            foreach(var m in mbers){
                res = AccountManager.RemoveUserInRole(Convert.ToInt32(m),Convert.ToInt32(role));
            }
            data[0]=res;
            break;
    }
    
     Json.Write(data,Response.Output);
    
}